<!-- 
 悬浮菜单拖拽组件 
 -->
<template>
	<view class="com-float-button" id="_drag_button" :style="'left: ' + left + 'px; top:' + top + 'px;'"
		@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
		:class="{transition: isDock && !isMove }">
		<view :class="{ mark: show }" @click="show = false"></view>
		<view :class="show ? 'list-active' : 'list'">
			<view class="list-item" :class="{ show: show }"
				:style="{ width: parseInt(size) + 'px', height: parseInt(size) + 'px', background: background }"
				v-for="(item, index) in list" :key="index" @click="clickItem(item)">
				<u-icon :name="item.icon" :color="item.color" :size="iconSize"></u-icon>
			</view>
		</view>
		<view class="list-item show no-mb"
			:style="{ width: parseInt(size) + 'px', height: parseInt(size) + 'px', background: background }"
			@click="show = !show">
			<u-icon :name="show ? 'close' : 'grid-fill'" :color="color" :size="iconSize"></u-icon>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			// 悬停时距离左右两边的距离
			edge: {
				type: Number,
				default: 12
			},
			// 距离底部安全距离
			safe_bottom: {
				type: Number,
				default: 20
			},
			// 是否开启吸附
			isDock: {
				type: Boolean,
				default: true
			},
			// 按钮宽高rpx
			size: {
				type: Number,
				default: 40
			},
			// 按钮背景颜色
			background: {
				type: String,
				default: 'rgba(0, 0, 0, 0.6)'
			}
		},
		data() {
			return {
				show: false,
				isMove: true,

				top: 0,
				left: 0,

				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				color: '#3c9cff',
				iconSize: '30',
				list: [{
						key: 'phone',
						icon: 'phone-fill',
						color: '#3c9cff'
					},
					{
						key: 'server',
						icon: 'server-fill',
						color: '#3c9cff'
					},
				],

				safe_top: 0,
			};
		},

		created() {
			this.setSafeTop();
		},
		mounted() {
			const sys = uni.getSystemInfoSync();
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				this.left = this.windowWidth - this.width - this.edge;
				this.top = this.windowHeight - this.height - this.edge - this.safe_bottom;
			}).exec();
		},
		methods: {
			// 设置按钮距离顶部安全距离
			setSafeTop() {
				let btnL = this.list.length;
				this.safe_top = (btnL + 1) * this.size;
			},
			touchstart(e) {},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				this.isMove = true;
				this.left = e.touches[0].clientX - this.offsetWidth;
				let clientY = e.touches[0].clientY - this.offsetHeight;
				let edgeBottom = this.windowHeight - this.height - this.safe_bottom;
				// 上下触及边界
				if (clientY < this.safe_top) {
					this.top = this.safe_top;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}
			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
				}
				this.isMove = false;
			},

			// 点击事件
			clickItem(item) {
				uni.showToast({
					title: item.key
				})
			},
		}
	};
</script>

<style scoped>
	.com-float-button {
		position: fixed;
		z-index: 999;
	}

	.mark {
		z-index: -1;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.38);
		transition: all 0.3s linear;
	}

	.list-active {
		position: absolute;
		bottom: 84rpx;
		left: 0;
		transform: scale(1);
		transition: all 0.3s ease-in-out;
	}

	.list {
		position: absolute;
		bottom: 0;
		left: 0;
		transform: scale(0);
		transform-origin: bottom;
		opacity: 0;
		transition: all 0.3s ease-in-out;
	}

	.list-item {
		margin-bottom: 20rpx;
		border-radius: 50%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
	}

	.no-mb {
		margin-bottom: 0;
	}

	.icon {
		width: 68rpx;
		height: 68rpx;
		display: block;
	}

	.transition {
		transition: left .3s ease, top .3s ease;
	}
</style>
